﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using DTO;
using AutoMapper;

namespace DataAccess
{
    public static class GoalTypeDAO
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        static GoalTypeDAO()
        {
            XmlConfigurator.Configure();
        }

        public static List<GoalTypeDTO> GetGoalTypeList()
        {
            try
            {
                List<GoalTypeDTO> lstGoalType = new List<GoalTypeDTO>();

                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = from GoalType in context.GOAL_TYPE_LIST
                            select GoalType;
                foreach (var item in query)
                {
                    GoalTypeDTO dtoGoalType = new GoalTypeDTO();
                    dtoGoalType = Mapper.Map<GOAL_TYPE, GoalTypeDTO>(item);
                    lstGoalType.Add(dtoGoalType);
                }
                return lstGoalType;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }
        }

        public static GoalTypeDTO FindGoalType(GoalTypeDTO dtoGoalType, string searchOption)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();

                IQueryable<GOAL_TYPE > query=null;
                if(searchOption ==GoalTypeColumns .GOAL_TYPE_ID )
                {
                   query = from GoalType in context.GOAL_TYPE_LIST
                            where GoalType.GOAL_TYPE_ID == dtoGoalType.GOAL_TYPE_ID
                            select GoalType;                    
                    
                }
                else if (searchOption == GoalTypeColumns.GOAL_TYPE_DESC)
                {
                    query = from GoalType in context.GOAL_TYPE_LIST
                            where GoalType.GOAL_TYPE_NAME == dtoGoalType.GOAL_TYPE_NAME
                            select GoalType;                    
                }

                if (query == null)
                {
                    logger.Debug("Chưa hỗ trợ tìm kiếm theo "+searchOption );
                    return null;
                }

                if (query.Count() == 0)
                    return null;

                GoalTypeDTO Result = new GoalTypeDTO();
                GOAL_TYPE goaltype = query.First();
                Result = Mapper.Map<GOAL_TYPE, GoalTypeDTO>(goaltype);
                return Result;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }
        }     

        public static bool AddNewGoalType(GoalTypeDTO dtoGoalType)
        {
            try
            {
                GOAL_TYPE obj = new GOAL_TYPE();
                //Mapping                
                obj = Mapper.Map<GoalTypeDTO, GOAL_TYPE>(dtoGoalType);
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                context.AddToGOAL_TYPE_LIST(obj);
                context.SaveChanges(true);
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool UpdateGoalType(GoalTypeDTO dtoGoalType)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                GOAL_TYPE GoalType = context.GOAL_TYPE_LIST.First(g => g.GOAL_TYPE_ID == dtoGoalType.GOAL_TYPE_ID);
                GoalType.GOAL_TYPE_NAME = dtoGoalType.GOAL_TYPE_NAME;
                GoalType.GOAL_TYPE_DESC  = dtoGoalType.GOAL_TYPE_DESC;
                context.SaveChanges(true);

                dtoGoalType.GOAL_TYPE_ID = GoalType.GOAL_TYPE_ID;
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAGoalType(GoalTypeDTO dtoGoalType)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                GOAL_TYPE GoalType = context.GOAL_TYPE_LIST.First(g => g.GOAL_TYPE_ID  == dtoGoalType.GOAL_TYPE_ID );
                context.DeleteObject(GoalType);
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAllGoalType()
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = (from GoalType in context.GOAL_TYPE_LIST
                             select GoalType);
                foreach (var item in query)
                {
                    context.DeleteObject(item);
                }
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }
       
    }

  
}
